Skip to content

More consistent checks for captured types - #1662

Merged
msridhar merged 3 commits into
masterfrom
captured-type-wildcard-containment
Jul 29, 2026
Merged

msridhar merged 3 commits into
masterfrom
captured-type-wildcard-containment

Conversation

@msridhar

@msridhar msridhar commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

This PR addresses this sub-issue of #1656:

CheckIdenticalNullabilityVisitor inconsistently recognizes wildcards. extendsBoundContains()
uses GenericsUtils.asWildcard(), which includes captured wildcards, but the surrounding logic and
superWildcardContains() use instanceof WildcardType or TypeKind.WILDCARD.

Captured wildcards can therefore bypass containment and nullability checks, especially for
? super cases.

The fix is to consistently call GenericsUtils.asWildcard, which handles both WildcardType and CapturedType, when checking if a type is a wildcard.

The new tests show subtyping issues that weren't identified before that are caught with these fixes.

Summary by CodeRabbit

  • Bug Fixes

    • Improved generic wildcard containment checks, including captured and nested super wildcards.
    • Correctly reports incompatible assignments involving recursive generic type inference.
    • Prevents incorrect recursive analysis for F-bounded type parameters.
  • Tests

    • Added coverage for captured wildcard returns, nested containment, and F-bounded generics.

@msridhar

Copy link
Copy Markdown
Contributor Author

This change is part of the following stack:

Change managed by git-spice.

@codecov

codecov Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.75%. Comparing base (01ed42e) to head (6993483).

Files with missing lines Patch % Lines
...way/generics/CheckIdenticalNullabilityVisitor.java 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1662      +/-   ##
============================================
+ Coverage     87.73%   87.75%   +0.02%     
- Complexity     3130     3132       +2     
============================================
  Files           109      109              
  Lines         10579    10580       +1     
  Branches       2137     2136       -1     
============================================
+ Hits           9281     9285       +4     
+ Misses          616      615       -1     
+ Partials        682      680       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar
msridhar marked this pull request as ready for review July 28, 2026 01:55
@msridhar
msridhar requested a review from lazaroclapp July 28, 2026 01:55
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

CheckIdenticalNullabilityVisitor now detects RHS wildcards through GenericsUtils.asWildcard, avoids applying that conversion to captured LHS type variables, and routes explicit wildcard cases through containment checks. Super-wildcard handling retains its existing bound-kind behavior. WildcardTests adds coverage for direct and recursive Box<? super ...> assignments through identity, plus an F-bounded type-parameter scenario.

Possibly related issues

Possibly related PRs

  • uber/NullAway#1547 — Both modify ? super wildcard containment logic and add corresponding WildcardTests coverage.
  • uber/NullAway#1548 — Both update typeArgumentContainedBy, wildcardContains, and superWildcardContains with wildcard variance tests.
  • uber/NullAway#1549 — Both refine wildcard detection and effective bound handling in CheckIdenticalNullabilityVisitor.

Suggested reviewers: lazaroclapp

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: making captured-type checks more consistent and covering wildcard/captured type handling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch captured-type-wildcard-containment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java`:
- Around line 906-980: Add concise Javadoc immediately above the three new
public test methods: capturedSuperWildcardReturnCheckedForContainment should
document direct wildcard containment checking,
capturedSuperWildcardReturnCheckedRecursively should document recursive nested
containment checking, and capturedLhsWithFBoundedTypeParametersDoesNotRecurse
should document that F-bounded type parameters must not trigger recursive
analysis.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 6194c562-6f3f-4a5a-b781-6108bbcc2b1b

📥 Commits

Reviewing files that changed from the base of the PR and between 01ed42e and 6993483.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java

Comment on lines +906 to +980
@Test
public void capturedSuperWildcardReturnCheckedForContainment() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
interface Box<T extends @Nullable Object> {}
static <T extends @Nullable Object> Box<T> identity(Box<T> box) {
return box;
}
void test(
Box<? super String> nonNullBoundBox,
Box<? super @Nullable String> nullableBoundBox) {
Box<? super String> ok = identity(nullableBoundBox);
// BUG: Diagnostic contains: incompatible types
Box<? super @Nullable String> bad = identity(nonNullBoundBox);
}
}
""")
.doTest();
}

@Test
public void capturedSuperWildcardReturnCheckedRecursively() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
interface Box<T extends @Nullable Object> {}
interface Nested<T extends @Nullable Object> {}
static <T extends @Nullable Object> Box<T> identity(Box<T> box) {
return box;
}
void test(Box<? super Nested<String>> box) {
// BUG: Diagnostic contains: incompatible types
Box<? super Nested<@Nullable String>> bad = identity(box);
}
}
""")
.doTest();
}

@Test
public void capturedLhsWithFBoundedTypeParametersDoesNotRecurse() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
@NullMarked
class Test {
interface Value<V extends Value<V>> {}
interface Store<S extends Store<S>> {}
interface Transfer<V extends Value<V>, S extends Store<S>> {}
static class Analysis<
V extends Value<V>,
S extends Store<S>,
T extends Transfer<V, S>> {
Analysis(T transfer) {}
}
static Analysis<?, ?, ?> create(Transfer<?, ?> transfer) {
return new Analysis<>(transfer);
}
}
""")
.doTest();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add Javadoc for the new regression tests.

Document the direct containment, recursive containment, and F-bounded recursion scenarios in the three new public test methods. As per coding guidelines, “Add Javadoc to every non-trivial method, including private methods.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java` around
lines 906 - 980, Add concise Javadoc immediately above the three new public test
methods: capturedSuperWildcardReturnCheckedForContainment should document direct
wildcard containment checking, capturedSuperWildcardReturnCheckedRecursively
should document recursive nested containment checking, and
capturedLhsWithFBoundedTypeParametersDoesNotRecurse should document that
F-bounded type parameters must not trigger recursive analysis.

Source: Coding guidelines

@msridhar
msridhar merged commit e4a3fb8 into master Jul 29, 2026
9 of 10 checks passed
@msridhar
msridhar deleted the captured-type-wildcard-containment branch July 29, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants